package com.lmpop.frame.bpm;

import com.lmpop.frame.QueryList;
import com.lmpop.frame.SysMap;
import com.lmpop.frame.TradeInterf;
import com.lmpop.frame.dao.DbmInterf;
import com.lmpop.frame.exp.ApplicationException;
import com.lmpop.frame.pojo.Bpmdefinition;
import java.util.List;
import java.util.Random;

public class BpmInfo
  implements TradeInterf
{
  QueryList queryList;

  public BpmInfo()
  {
    this.queryList = new QueryList();
  }

  public SysMap delete(DbmInterf dbmMgr, SysMap map)
  {
    return null;
  }

  public int getCountByObj(Object obj)
  {
    return 0;
  }

  public List getListByPage(Object obj, int start, int limit)
  {
    return null;
  }

  public List getListByPk(String id) {
    return this.queryList.getList("getBpm", id);
  }

  public List getBpmListByBpmId(String bpm_id) {
	  //select t.trade_type_code,t.bpm_id,t.trade_name,t.judge_rights,t.trade_kind,b.node_sequence,b.node_class,b.node_method from ts_tradetype t,ts_bpmdefinition b where t.bpm_id=b.bpm_id and t.trade_type_code=#bpm_id# order by b.node_sequence desc
    return this.queryList.getList("getBpmListByBpmId", bpm_id);
  }

  public SysMap insert(DbmInterf dbmMgr, SysMap map) {
    String result_code = "0";
    try {
      String table_name = map.getString("table_name");
      String className = "";
      if ((!table_name.equals("")) && (table_name.length() >= 1)) {
        className = table_name.substring(0, 1).toUpperCase() + table_name.substring(1, table_name.length());
      }
      String package_name = "com.lmpop.trade." + table_name + "." + className + "Info";
      String insert_bpmid = getFourRandnum();
      String update_bpmid = getFourRandnum();
      String delete_bpmid = getFourRandnum();

      Bpmdefinition bpmdefinition = new Bpmdefinition(insert_bpmid, package_name, "insert");
      Bpmdefinition bpmdefinition1 = new Bpmdefinition(update_bpmid, package_name, "update");
      Bpmdefinition bpmdefinition2 = new Bpmdefinition(delete_bpmid, package_name, "delete");

      String createBpm = map.getString("createBpm");

      if (createBpm.equals("1")) {
        dbmMgr.update("insertBpmdefinition", bpmdefinition);
        dbmMgr.update("insertBpmdefinition", bpmdefinition1);
        dbmMgr.update("insertBpmdefinition", bpmdefinition2);
      }

      map.setString("result_code", result_code);

      map.setString("insert_bpmid", insert_bpmid);
      map.setString("update_bpmid", update_bpmid);
      map.setString("delete_bpmid", delete_bpmid);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    return map;
  }

  public String getFourRandnum() {
    String[] num = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
    Random r = new Random();
    StringBuffer result = new StringBuffer();
    for (int i = 0; i < 4; i++) {
      result.append(num[r.nextInt(10)]);
    }
    if (isExistBpmid(result.toString())) getFourRandnum();
    return result.toString();
  }

  public boolean isExistBpmid(String module_id) {
    List list = this.queryList.getList("isExistBpmid", module_id);

    return (list != null) && (list.size() > 0);
  }

  public SysMap update(DbmInterf dbmMgr, SysMap map)
  {
    return null;
  }
}